package com.platform.cdcs.activity;

import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.os.Vibrator;
import android.support.annotation.NonNull;
import android.support.v4.content.ContextCompat;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.google.zxing.client.android.DecoderMode;
import com.platform.cdcs.R;
import com.platform.cdcs.adapter.ScanItemAdapter;
import com.platform.cdcs.fragment.ProductFragment;
import com.platform.cdcs.fragment.stock.AddProductFragment;
import com.platform.cdcs.fragment.stock.EditProductFragment;
import com.platform.cdcs.model.ProductList;
import com.platform.cdcs.model.RefershEvent;
import com.platform.cdcs.tool.CacheTool;
import com.platform.cdcs.tool.Constant;
import com.platform.cdcs.tool.FragmentUtil;
import com.platform.cdcs.tool.ViewTool;
import com.platform.cdcs.widget.CodeDemoWindow;
import com.platform.cdcs.widget.FormDialog;
import com.trueway.app.uilib.model.ChooseItem;
import com.trueway.app.uilib.tool.Utils;
import com.zhy.http.okhttp.callback.StringCallback;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

import de.greenrobot.event.EventBus;
import okhttp3.Call;

/**
 * Created by holytang on 2017/9/30.
 */
public class StockScanActivity extends ScanBaseActivity {

    private int model;
    private int position;
    private int[] demo = new int[]{R.mipmap.unique_demo, R.mipmap.box_demo, R.mipmap.gs1_demo};
    private TextView titleView;
    private String pos2Code1 = "", pos2Code2 = "";
    private String pos3Code1 = "", pos3Code2 = "", pos3Date = "";
    private String pos1Code;
    private View.OnClickListener tipListener = new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            if (view.getId() == R.id.button2) {
                new CodeDemoWindow(getActivity()).setImg(demo[position], view);
            } else {
                String[] forms = null;
                String title = null;
                int type = 0;
                if (position == 0) {
                    pos1Code = "";
                    forms = new String[]{"唯一码"};
                    title = "唯一码";
                } else if (position == 1) {
                    pos2Code1 = "";
                    pos2Code2 = "";
                    forms = new String[]{"运单号", "箱号"};
                    title = "箱码";
                } else {
                    pos3Code1 = "";
                    pos3Code2 = "";
                    title = "GS1码";
                    type = 1;
                    forms = new String[]{"01-[GTIN]", "10-[批号]", "11-[生产日期]", "17-[失效日期]", "20-[变种]", "21-[唯一标识]"};
                }
                new FormDialog(getActivity(), new FormDialog.FormListener() {
                    @Override
                    public void okClick(String[] value) {
//                        addStock(value);
                        if (position == 0) {
                            pos1Code = value[0];
                            postScan();
                        } else if (position == 1) {
                            pos2Code1 = value[0];
                            pos2Code2 = value[1];
                            addStock(null, value);
                        } else {
                            pos3Code1 = value[0];
                            pos3Code2 = value[1];
                            addStock(null, value);
                        }
                    }
                }, title).addView(forms, type, "").show();
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        model = getIntent().getIntExtra("model", 0);
    }

    protected void modeInit() {
        if (getHandler() != null) {
            getHandler().postDelayed(new Runnable() {
                @Override
                public void run() {
                    changeScanMode(DecoderMode.Zxing);
                }
            }, 1000);
        }
    }

    @Override
    protected void initLayout() {
        Window window = getWindow();
        window.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
        setContentView(R.layout.stock_scan);
    }

    private void initTip(TextView textViews1, TextView textView2, String[] tip) {
        titleView.setText(Constant.SCAN_TITLES[position + 1]);
        textViews1.setText(tip[0]);
        textView2.setText(tip[1]);
    }

    @Override
    protected void initView() {
        super.initView();
        initLoadImg(findViewById(R.id.load));
        getToolBar().setNavigationIcon(R.mipmap.icon_back);
        getToolBar().setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        setTitle("扫码添加");
        titleView = (TextView) findViewById(R.id.text);
        GridView tabView = (GridView) findViewById(R.id.grid);
        final ScanItemAdapter tabAdapter = new ScanItemAdapter(getActivity());
        String[] titles = new String[]{"唯一码", "箱码", "GS1码"};
        final Map<Integer, String[]> tipMap = new HashMap<>();
        tipMap.put(0, new String[]{"唯一码示例", "无法识别？"});
        tipMap.put(1, new String[]{"箱码示例", "无法识别？"});
        tipMap.put(2, new String[]{"GS1码示例", "无法识别？"});
        int[] icons = new int[]{R.mipmap.icon_code_1_normal, R.mipmap.icon_code_1_normal, R.mipmap.icon_code_2_normal};
        int[] selectedIcons = new int[]{R.mipmap.icon_code_1_press, R.mipmap.icon_code_1_press, R.mipmap.icon_code_2_press};

        for (int i = 0; i < icons.length; i++) {
            ChooseItem item = new ChooseItem();
            item.setTitle(titles[i]);
            item.setDrawable(icons[i]);
            item.setType(selectedIcons[i]);
            tabAdapter.addItem(item);
        }
        tabView.setAdapter(tabAdapter);
        final TextView tip1 = (TextView) findViewById(R.id.button2);
        final TextView tip2 = (TextView) findViewById(R.id.button3);
        tip1.setOnClickListener(tipListener);
        tip2.setOnClickListener(tipListener);
        initTip(tip1, tip2, tipMap.get(0));
        tabView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                tabAdapter.setChoose(i);
                tabAdapter.notifyDataSetChanged();
                position = i;
                initTip(tip1, tip2, tipMap.get(position));
            }
        });

//        findViewById(R.id.bar).setVisibility(View.VISIBLE);
//        TextView titleView = (TextView) findViewById(R.id.bottom).findViewById(R.id.title);
//        titleView.setVisibility(View.VISIBLE);
//        if(model==0){
//            titleView.setText(String.format("已添加%s 项", CacheTool.getInputCount(getActivity())));
//        }else{
//            titleView.setText(String.format("已添加%s 项", CacheTool.getOutputCount(getActivity())));
//        }
//        TextView btn = (TextView) findViewById(R.id.button1);
//        btn.setText("结束添加");
//        LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
//        int margin = Utils.convertDIP2PX(getActivity(), 12);
//        lp.setMargins(margin, margin, margin, margin);
//        btn.setLayoutParams(lp);
//        btn.setVisibility(View.VISIBLE);
//        btn.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View view) {
//                finishAdd();
//            }
//        });

    }

    private void finishAdd() {
        Bundle bundle = new Bundle();
        bundle.putInt("model", model);
        FragmentUtil.navigateToInNewActivity(this, EditProductFragment.class, bundle);
        finish();
    }

    @Override
    protected void initMenu(Menu menu) {
        menu.add(0, 0, 0, "已扫").setTitle("已扫").setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(MenuItem menuItem) {
                finishAdd();
                return false;
            }
        }).setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
        menu.add(0, 0, 0, "手动添加").setTitle("手动添加").setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(MenuItem menuItem) {
                Bundle bundle = new Bundle();
                bundle.putInt("model", model);
                FragmentUtil.navigateToInNewActivity(getActivity(), AddProductFragment.class, bundle);
                finish();
                return false;
            }
        }).setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
    }

    private void vibrate() {
        Vibrator vibrator = (Vibrator) getSystemService(VIBRATOR_SERVICE);
        vibrator.vibrate(200);
    }

    public void onScanQRCodeSuccess(String result) {
        vibrate();
        scanResult(result);
    }

    private void addStock(ProductList.ProductItem item, String[] values) {
        LinearLayout rootView = (LinearLayout) findViewById(R.id.root);
        rootView.setVisibility(View.VISIBLE);
        LayoutInflater inflater = LayoutInflater.from(getActivity());
        rootView.removeAllViews();
        findViewById(R.id.bar).setVisibility(View.VISIBLE);
        findViewById(R.id.customPanel).setVisibility(View.GONE);
        if (position == 0) {
            ViewTool.setFourItemOnly(inflater, rootView, new String[]{"产品名称", item.getMaterialName()});
            ViewTool.setFourItem(inflater, rootView, new String[]{"产品序列号", "产品线"}, new String[]{item.getSerialNumber(), item.getSubBU()});
            ViewTool.setFourItem(inflater, rootView, new String[]{"产品批号", "数量"}, new String[]{item.getItemCode(), item.getQty() + " " + item.getUom()});
            TextView titleView = (TextView) findViewById(R.id.bottom).findViewById(R.id.title);
            titleView.setVisibility(View.VISIBLE);
            titleView.setText(String.format("已添加%s 项", CacheTool.getInputCount(getActivity())));
            TextView btn = (TextView) findViewById(R.id.button1);
            btn.setText("结束添加");
            LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
            int margin = Utils.convertDIP2PX(getActivity(), 12);
            lp.setMargins(margin, margin, margin, margin);
            btn.setLayoutParams(lp);
            btn.setVisibility(View.VISIBLE);
            btn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    finishAdd();
                }
            });
        } else if (position == 1) {
            final TextView[] tv = ViewTool.setFourItem(inflater, rootView, new String[]{"运单号", pos2Code1});
            tv[1].setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    new FormDialog(getActivity(), new FormDialog.FormListener() {
                        @Override
                        public void okClick(String[] value) {
                            tv[1].setText(value[0]);
                            pos2Code1 = value[0];
                            setBtnEnable(pos2Code1, pos2Code2);
                        }
                    }, "运单号").addView(new String[]{""}, 0, pos2Code1).show();
                }
            });
            final TextView[] tv1 = ViewTool.setFourItem(inflater, rootView, new String[]{"箱号", pos2Code2});
            tv1[1].setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    new FormDialog(getActivity(), new FormDialog.FormListener() {
                        @Override
                        public void okClick(String[] value) {
                            tv1[1].setText(value[0]);
                            pos2Code2 = value[0];
                            setBtnEnable(pos2Code1, pos2Code2);
                        }
                    }, "箱号").addView(new String[]{""}, 0, pos2Code2).show();
                }
            });
            TextView titleView = (TextView) findViewById(R.id.bottom).findViewById(R.id.title);
            titleView.setVisibility(View.GONE);
            TextView btn = (TextView) findViewById(R.id.button1);
            btn.setText("扫描完成");
            LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
            int margin = Utils.convertDIP2PX(getActivity(), 12);
            lp.setMargins(margin, margin, margin, margin);
            btn.setLayoutParams(lp);
            setBtnEnable(pos2Code1, pos2Code2);
            btn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    postScan();
                }
            });
        } else {
            final TextView[] tv = ViewTool.setFourItem(inflater, rootView, new String[]{"01-[GTIN]", "10-[批号]"}, new String[]{pos3Code1, pos3Code2}, true);
            tv[0].setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    new FormDialog(getActivity(), new FormDialog.FormListener() {
                        @Override
                        public void okClick(String[] value) {
                            tv[0].setText(value[0]);
                            pos3Code1 = value[0];
                            setBtnEnable(pos3Code1, pos3Code2);
                        }
                    }, "GTIN").addView(new String[]{"GTIN"}, 0, pos3Code1).show();
                }
            });
            tv[1].setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    new FormDialog(getActivity(), new FormDialog.FormListener() {
                        @Override
                        public void okClick(String[] value) {
                            tv[1].setText(value[0]);
                            pos3Code2 = value[0];
                            setBtnEnable(pos3Code1, pos3Code2);
                        }
                    }, "批号").addView(new String[]{"批号"}, 0, pos3Code2).show();
                }
            });
            if (values == null) {
                values = new String[]{"", "", "", pos3Date, "", ""};
            }
            ViewTool.setFourItem(inflater, rootView, new String[]{"11-[生产日期]", "17-[失效日期]"}, new String[]{values[2], values[3]});
            ViewTool.setFourItem(inflater, rootView, new String[]{"20-[变种]", "21-[唯一标识]"}, new String[]{values[4], values[5]});
            TextView titleView = (TextView) findViewById(R.id.bottom).findViewById(R.id.title);
            titleView.setVisibility(View.GONE);
            TextView btn = (TextView) findViewById(R.id.button1);
            btn.setText("扫描完成");
            LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
            int margin = Utils.convertDIP2PX(getActivity(), 12);
            lp.setMargins(margin, margin, margin, margin);
            btn.setLayoutParams(lp);
            setBtnEnable(pos3Code1, pos3Code2);
            btn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    postScan();
                }
            });
        }
    }

    private void scanResult(String code) {
        if (position == 0) {
            pos1Code = code;
            postScan();
        } else if (position == 1) {
            if (code.length() == 10) {
                pos2Code1 = code;
//                Utils.showToast(getActivity(), "已扫描运单号");
            } else if (code.length() == 4) {
                pos2Code2 = code;

//                Utils.showToast(getActivity(), "已扫描箱号");
            }
//            if (!TextUtils.isEmpty(pos2Code1) && !TextUtils.isEmpty(pos2Code2)) {
//                postScan();
//            } else {
            addStock(null, null);
            startSpot();
//            }
        } else {
            if (code.startsWith("01") && code.length() >= 16) {
                pos3Code1 = code.substring(2, 16);
            } else if (code.startsWith("17")) {
                if (code.length() > "1719013110".length()) {
                    pos3Code2 = code.substring("1719013110".length());
                    pos3Date = code.substring(2, 8);
                }
            }
            addStock(null, null);
            startSpot();
        }
    }

    private void postScan() {
        showLoadImg();
        Map<String, String> param = new HashMap<>();
        param.put("type", model == 0 ? "2" : "3");
        if (position == 0) {
            param.put("serialNumber", pos1Code);
        } else if (position == 1) {
            param.put("packageNumber", pos2Code2);
            param.put("deliveryNumber", pos2Code1);
        } else {
            param.put("gtin", pos3Code1);
            param.put("lots", pos3Code2);
        }
        getHttpClient().post().url(Constant.SCAN_PRODUCT).params(Constant.makeParam(param)).build().execute(new StringCallback() {

            @Override
            public void onError(Call call, Exception e, int i) {
                dismissLoadImg();
                Utils.showToast(getContext(), R.string.server_error);
                startSpot();
            }

            @Override
            public void onResponse(String s, int i) {
                try {
                    if (getActivity() == null) {
                        return;
                    }
                    dismissLoadImg();
                    startSpot();
                    JSONObject data = new JSONObject(s);
                    String code = "";
                    String msg = "";
                    JSONArray array = null;
                    if (data.has("result")) {
                        code = data.getJSONObject("result").getString("code");
                        msg = data.getJSONObject("result").has("msg") ? data.getJSONObject("result").getString("msg") : "";
                        array = data.getJSONObject("result").has("objList") ? data.getJSONObject("result").getJSONArray("objList") : new JSONArray();
                    } else {
                        code = data.getString("code");
                        msg = data.has("msg") ? data.getString("msg") : "";
                        array = data.has("objList") ? data.getJSONArray("objList") : new JSONArray();
                    }

                    if (!"1".endsWith(code)) {
                        Utils.showToast(getActivity(), msg);
                        return;
                    }
//                    if (position == 0) {
//                        parseOne(data.getJSONObject("result").getJSONArray("objList"));
//                    } else if (position == 1) {
//                        parseBox();
//                    } else {
//                        parseGS();
//                    }
                    if (array.length() == 0) {
                        Utils.showToast(getContext(), "产品不存在");
                        return;
                    }
                    parseOne(array);
                } catch (Exception e) {
                    Utils.showToast(getActivity(), "解析数据失败！");
                }
            }
        });
//        resetCode();
    }

    private void resetCode() {
        pos1Code = "";
        pos2Code1 = "";
        pos2Code2 = "";
        pos3Code1 = "";
        pos3Code2 = "";
    }

    private void parseOne(JSONArray array) throws JSONException {
        ProductList.ProductItem item = null;
        boolean flag = false;
        for (int i = 0; i < array.length(); i++) {
            JSONObject obj = array.getJSONObject(i);
            item = new ProductList.ProductItem();
            item.setMaterialName(obj.getString("materialName"));
            item.setMaterialNumber(obj.getString("materialNumber"));
            item.setSerialNumber(obj.getString("serialNumber"));
            item.setLots(obj.getString("batchNumber"));
            item.setSubBU(obj.getString("subBU"));
            item.setUom(obj.getString("saleUnit"));
            item.setQty(obj.getString("qty"));
            item.setNowqty(obj.getString("qty"));
            item.setProductId(obj.getString("productId"));
            item.setExpDate(obj.getString("expDate"));
            item.setDomDate(obj.getString("dom_date"));
            item.setDistCode(obj.getString("distCode"));
            item.setDistName(obj.getString("distName"));
//            item.setQty("1");
            if (CacheTool.append(getActivity(), item, model) == null) {
                flag = true;
            }
            if (i == array.length() - 1) {
                CacheTool.saveCache(getActivity(), model);
            }
        }
        RefershEvent event = new RefershEvent();
        event.mclass = ProductFragment.class;
        EventBus.getDefault().post(event);
        if (flag) {
            Utils.showToast(getContext(), model == 0 ? "产品已添加到本地入库列表" : "产品已添加到本地出库列表");
            return;
        }
        if (position == 0 && item != null) {
            addStock(item, null);
        } else if (position == 1) {
            finishAdd();
        } else if (position == 2) {
            finishAdd();
        }
    }

    private void parseBox() {

    }

    private void parseGS() {

    }

    private void setBtnEnable(String value1, String value2) {
        if (TextUtils.isEmpty(value1) || TextUtils.isEmpty(value2)) {
            findViewById(R.id.bar).setEnabled(false);
            findViewById(R.id.bar).setBackgroundColor(ContextCompat.getColor(getContext(), R.color.text_gray));
        } else {
            findViewById(R.id.bar).setEnabled(true);
            findViewById(R.id.bar).setBackgroundResource(R.drawable.shape_corner);
        }
        findViewById(R.id.bar).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                postScan();
            }
        });
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        if (grantResults.length > 0 && requestCode == Constant.PERMISSION_CAMERA) {
            if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                Intent intent = new Intent(this, StockScanActivity.class);
                intent.putExtra("model", model);
                startActivity(intent);
                finish();
            } else {
                finish();
                Utils.showToast(getActivity(), "请先打开摄像头权限！");
            }
        }
    }
}
